package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;




    @PostMapping
    public R<String> save(@RequestBody Category category) {

        categoryService.save(category);

        return R.success("添加成功！");
    }


    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){

        Page<Category> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>();
        wrapper.orderByDesc(Category::getUpdateTime);

       categoryService.page(page1, wrapper);



        return R.success(page1);

    }



    @DeleteMapping
    public R<String> DeleteById( Long id){


        categoryService.remove(id);

        return R.success("删除成功！");

    }


    @PutMapping
    public R<String> update(@RequestBody Category category){

        categoryService.updateById(category);

        return R.success("修改成功！");



    }

    @GetMapping("/list")
    public R<List<Category>> list( Category category){


        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>();

        wrapper.eq(category.getType()!=null,Category::getType,category.getType());

        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(wrapper);


        return R.success(list);


    }



}
